package me.pplay.playtalk.activity.setting;

import me.pplay.playtalk.ApplicationTrans;
import me.pplay.playtalk.R;
import me.pplay.playtalk.activity.BaseActivity;
import me.pplay.playtalk.protocol.DataCallBack;
import me.pplay.playtalk.protocol.TpChat;
import me.pplay.playtalk.protocol.bean.RspData;
import me.pplay.playtalk.util.LogManager;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;

public class SettingChPwdActivity extends BaseActivity implements OnClickListener {
	EditText set_changepassword_old,set_changepassword_new,set_changepassword_confirm;
	Button set_changepassword_btn;
	private ProgressDialog progressDialog=null;
	Handler handler = new Handler() {

		@Override
		public void handleMessage(Message msg) {
			// TODO Auto-generated method stub
			super.handleMessage(msg);
			if (progressDialog != null)
				progressDialog.dismiss();
			LogManager.LogShow("SettingChPwdActivity", "handleMessage msg.what = " + msg.what);
			switch (msg.what) {
			case TpChat.CHAT_CHPWD_TIMEOUT :
				showToast("Time out");
				break;
			case TpChat.CHAT_CHPWD_ERR:
				LogManager.LogShow("SettingChPwdActivity", "handleMessage AUTH_GETPWDPROTECTION_ERR");
				RspData rspdata = (RspData) msg.obj;
				showToast(rspdata.getString() == null ? "Unkown err" : rspdata.getString());
				break;
			case TpChat.CHAT_CHPWD_OK:
				showToast("Done");
				SettingChPwdActivity.this.finish();
			default:
				break;
			}
		}

	};
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.layout_setting_changepassword);
		super.SetOnBackClickListener((ImageButton)findViewById(R.id.btn_back));
		set_changepassword_old = (EditText) findViewById(R.id.set_changepassword_old);
		set_changepassword_new = (EditText) findViewById(R.id.set_changepassword_new);
		set_changepassword_confirm = (EditText) findViewById(R.id.set_changepassword_confirm);
		set_changepassword_btn = (Button) findViewById(R.id.set_changepassword_btn);
		clickEvents();
	}
	private void clickEvents(){
		set_changepassword_btn.setOnClickListener(this);
	}
	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.set_changepassword_btn: {
			String oldpassword = set_changepassword_old.getText().toString().trim();
			String password = set_changepassword_new.getText().toString().trim();
			String confirmpassword = set_changepassword_confirm.getText().toString().trim();
			if (oldpassword.equals("") || password.equals("") || confirmpassword.equals("")) {
				showToast(getString(R.string.string_isnull));
				return;
			}
			if (!password.equals(confirmpassword)) {
				showToast(getString(R.string.password_iswrong));
				return;
			}
			progressDialog = ProgressDialog.show(SettingChPwdActivity.this, getString(R.string.loading), getString(R.string.waittng), true, false); 
			progressDialog.show();
			changepwd(oldpassword,password);
		}
			break;
		}
	}
//	private void showToast(String str) {
//		Toast.makeText(SettingChPwdActivity.this, str, Toast.LENGTH_SHORT).show();
//	}
	
	private void changepwd(String old_pass,String new_pass) 
	{
		ApplicationTrans appState = (ApplicationTrans) getApplicationContext();
		TpChat tpChat = appState.getTpChat();

		tpChat.tp_chat_change_passwd(old_pass, new_pass,new DataCallBack() {
			
			@Override
			public void Callback(int result, RspData resultdata, Object obj) {
				Message msg = new Message(); 
				msg.what = result; 
				msg.obj = resultdata;
				switch (result) {
				case TpChat.CHAT_CHPWD_TIMEOUT :
					break;
				case TpChat.CHAT_CHPWD_ERR:
					break;
				case TpChat.CHAT_CHPWD_OK:
					msg.obj = obj;
					break;
				}
				handler.sendMessage(msg);
			}
		});
	}
}
